<layout name='Layout/layout_popup' />
<script type="text/javascript">
	function save() {
		$("#rc select option").each(function(i) {
			email = ($(this).html().split(";")[1].split("&")[0]);
			emp_no = $(this).val();
			name = jQuery.trim($(this).text());
			name = name.replace(/<.*>/, '');
			html = "<span email=\"" + email + "\" emp_no=\"" + emp_no + "\"><nobr><b  title=\"" + email + "\">" + name + "</b><a class=\"del\" title=\"删除\"><i class=\"icon-remove\"></i></a></nobr></span>";			$("#recever .address_list", parent.document).append(html);
		});
		$("#cc select option").each(function(i) {
			email = ($(this).html().split(";")[1].split("&")[0]);
			emp_no = $(this).val();
			name = jQuery.trim($(this).text());
			name = name.replace(/<.*>/, '');
			html = "<span email=\"" + email + "\" emp_no=\"" + emp_no + "\"><nobr><b  title=\"" + email + "\">" + name + "</b><a class=\"del\" title=\"删除\"><i class=\"icon-remove\"></i></a></nobr></span>";
			$("#carbon_copy .address_list", parent.document).append(html);
		});
		$("#bcc select option").each(function(i) {
			email = ($(this).html().split(";")[1].split("&")[0]);
			emp_no = $(this).val();
			name = jQuery.trim($(this).text());
			name = name.replace(/<.*>/, '');
			html = "<span email=\"" + email + "\" emp_no=\"" + emp_no + "\"><nobr><b  title=\"" + email + "\">" + name + "</b><a class=\"del\" title=\"删除\"><i class=\"icon-remove\"></i></a></nobr></span>";
			$("#blind_carbon_copy .address_list", parent.document).append(html);
		});
		myclose();
	}

	// 显示AJAX 读取的数据
	function showdata(result) {
		$("#addr_list").html("");
		if ( type = $("input[name='type']:checked").val() == "company") {
			var id = $("#company a.active").attr("node");
			var dept_name = $("#company a.active span").text();
			var email = "dept@group";
			var html_string = "<label><span class=\"checkbox\"><input type=\"checkbox\" name=\"addr_id\" value=\"dept_" + id + "\"></span><div class=\"wrap\"><a id=\"D" + id + "\" title=" + email + " onmousedown=\"return false\" href=\"javascript:void(0);\">" + dept_name + "&lt;" + email + "&gt; </a></div></label>";
			$("#addr_list").html(html_string);
		}
		for (s in result.data) {
			var id = result.data[s].id;
			var position_name = result.data[s].position_name;
			var emp_no = result.data[s].emp_no;
			var name = result.data[s].name;
			var email = result.data[s].email;
			if (position_name == undefined) {
				var html_string = "<label><span class=\"checkbox\"><input type=\"checkbox\" name=\"addr_id\" value=\"" + emp_no + "\"></span><div class=\"wrap\"><a  title=" + email + " onmousedown=\"return false\" href=\"javascript:void(0);\">" + name + " &lt;" + email + "&gt; </a></div></label>";
			} else {
				var html_string = "<label><span class=\"checkbox\"><input type=\"checkbox\" name=\"addr_id\" value=\"" + emp_no + "\"></span><div class=\"wrap\"><a  title=" + email + " onmousedown=\"return false\" href=\"javascript:void(0);\">" + name + " / " + position_name + " &lt;" + email + "&gt; </a></div></label>";
			}
			$("#addr_list").html($("#addr_list").html() + html_string);
		}
		$("#addr_list input").iCheck({
			checkboxClass : 'icheckbox_minimal-grey',
			radioClass : 'iradio_minimal-grey',
			increaseArea : '20%' // optional
		});
	}


	$(document).ready(function() {

		$("#rb_{$type}").iCheck('check');
		// 选择用户默认选择的类型
		$("#{$type}").removeClass("display-none");
		$("input[name='type']").on('ifChanged', function() {
			$("input[name='type']").each(function() {
				$("#" + $(this).val()).addClass("display-none");
			});
			$("#" + $(this).val()).removeClass("display-none")
		})

		$(".tree_menu  a").click(function() {
			$(".tree_menu a").attr("class", "");
			var type = $("input[name='type']:checked").val();
			$(this).attr("class", "active");
			sendAjax("{:U('read')}", "type=" + type + "&id=" + $(this).attr("node"), function(data) {
				showdata(data);
			})
			return false;
			//禁止连接生效
		});

		$("#addr_list a").live("dblclick", function() {
			$text = $(this).text();
			$val = $(this).parents("label").find("input").val();
			if ($("#rc select option[value='" + $val + "']").val() == undefined) {
				$option = $("<option></option>");
				$option.val($val);
				$option.text($text).appendTo("#rc select");
				$("#rc_count").text("(" + $("#rc select option").length + ")");
			};
		});

		$("#rc  select").live("dblclick", function() {
			$(this).find("option:selected").remove();
			$("#rc_count").text("(" + $("#rc select option").length + ")");
		});

		$("#cc  select").live("dblclick", function() {
			$(this).find("option:selected").remove();
			$("#cc_count").text("(" + $("#cc select option").length + ")");
		});

		$("#bcc  select").live("dblclick", function() {
			$(this).find("option:selected").remove();
			$("#bcc_count").text("(" + $("#bcc select option").length + ")");
		});
		$("#addr_list").live("mouseover", function() {
			$("#addr_list label").draggable({
				appendTo : "body",
				helper : "clone"
			});
		});
		$("#rc select").droppable({
			activeClass : "ui-state-default",
			hoverClass : "ui-state-hover",
			accept : ":not(.ui-sortable-helper)",
			drop : function(event, ui) {
				$text = ui.draggable.text();
				$val = ui.draggable.find("input").val();
				if ($("#rc select option[value='" + $val + "']").val() == undefined) {
					$option = $("<option></option>");
					$option.val($val);
					$option.text($text).appendTo(this);
					$("#rc_count").text("(" + $("#rc select option").length + ")");
				};
			},
		}).sortable({
			items : "option:not(.placeholder)",
			sort : function() {
				$(this).removeClass("ui-state-default");
			}
		});
		$("#cc select").droppable({
			activeClass : "ui-state-default",
			hoverClass : "ui-state-hover",
			accept : ":not(.ui-sortable-helper)",
			drop : function(event, ui) {
				$text = ui.draggable.text();
				$val = ui.draggable.find("input").val();
				if ($("#cc select option[value='" + $val + "']").val() == undefined) {
					$option = $("<option></option>");
					$option.val($val);
					$option.text($text).appendTo(this);
					$("#cc_count").text("(" + $("#cc select option").length + ")");
				};
			},
		}).sortable({
			items : "li:not(.placeholder)",
			sort : function() {
				$(this).removeClass("ui-state-default");
			}
		});
		$("#bcc select").droppable({
			activeClass : "ui-state-default",
			hoverClass : "ui-state-hover",
			accept : ":not(.ui-sortable-helper)",
			drop : function(event, ui) {
				$text = ui.draggable.text();
				$val = ui.draggable.find("input").val();
				if ($("#bcc select option[value='" + $val + "']").val() == undefined) {
					$option = $("<option></option>");
					$option.val($val);
					$option.text($text).appendTo(this);
					$("#bcc_count").text("(" + $("#bcc select option").length + ")");
				};
			},
		}).sortable({
			items : "li:not(.placeholder)",
			sort : function() {
				$(this).removeClass("ui-state-default");
			}
		});
	})
	//最终确认

	function add_address(name) {
		$("input:checked[name='addr_id']").each(function() {
			$text = $(this).parents("label").find("a").text();
			$val = $(this).val();
			if ($("#" + name + " select option[value='" + $val + "']").val() == undefined) {
				$option = $("<option></option>");
				$option.val($val);
				$option.text($text).appendTo("#" + name + " select");
				$("#" + name + "_count").text("(" + $("#" + name + " select option").length + ")");
			};
		})
	}

	//-->
</script>
<div class="panel panel-default ">
	<div class="panel-heading clearfix">
		<div class="pull-left">
			<span class="checkbox">
				<input  type="radio" id="rb_company" name="type" value="company" >
			</span>
			<label class="radio-inline" for="rb_company">公司</label>
			<span class="checkbox">
				<input  type="radio" id="rb_rank" name="type" value="rank">
			</span>
			<label class="radio-inline" for="rb_rank">职级</label>
			<span class="checkbox">
				<input  type="radio" id="rb_position" name="type" value="position">
			</span>
			<label class="radio-inline" for="rb_position">职位</label>
			<span class="checkbox">
				<input type="radio" id="rb_personal" name="type" value="personal">
			</span>
			<label class="radio-inline" for="rb_personal"> 个人</label>
		</div>
		<div class="pull-right">
			<a  onclick="save();" class="btn btn-default">确定</a>
			<a  onclick="myclose();" class="btn btn-default">关闭</a>
		</div>
	</div>
	<div class="panel-body">
		<div class="col-28 pull-left">
			<div class="">
				<b>地址簿</b>
			</div>
			<div class="popup_tree_menu" >
				<div id="company" class="display-none" style="height:200px;">
					{$list_company}
				</div>
				<div id="rank" class="display-none" style="height:200px;">
					{$list_rank}
				</div>
				<div id="position" class="display-none" style="height:200px;">
					{$list_position}
				</div>
				<div id="personal" class="display-none" style="height:200px;">
					<ul class="tree_menu">
						<volist name="list_personal" id="vo">
							<li>
								<a class="" node="{$key}"><i class="icon level1"></i><span>{$vo}</span></a>
							</li>
						</volist>
					</ul>
				</div>
			</div>
			<div>
				<div id="addr_list" style="width:100%;height:210px;"></div>
			</div>
		</div>
		<div class="col-34 pull-right">
			<div>
				<b style="padding-left:60px;">收件人</b><span id="rc_count"></span>
			</div>
			<div class="clearfix" style="margin-bottom: 15px;">
				<label class="col-4 pull-left text-right" ><a onclick="add_address('rc');" class="btn btn-default"> <i class="icon-double-angle-right"></i> </a> </label>
				<div class="col-28 pull-right">
					<div id="rc" style="width:100%;height:130px;overflow:hidden">
						<select size="6" style="height:100%;width:100%;"></select>
					</div>
				</div>
			</div>

			<div>
				<b  style="padding-left:60px;">抄送</b><span id="cc_count"></span>
			</div>
			<div class="clearfix" style="margin-bottom: 15px;">
				<label class="col-4 pull-left text-right" ><a onclick="add_address('cc');" class="btn btn-default"> <i class="icon-double-angle-right"></i></a></label>
				<div class="col-28 pull-right">
					<div id="cc" style="width:100%;height:130px;overflow:hidden">
						<select size="6" style="height:100%;width:100%;"></select>
					</div>
				</div>
			</div>
			<div>
				<b  style="padding-left:60px;">密送</b><span id="cc_count"></span>
			</div>
			<div class="clearfix">
				<label class="col-4 pull-left text-right" ><a onclick="add_address('bcc');" class="btn btn-default"> <i class="icon-double-angle-right"></i></a></label>
				<div class="col-28 pull-right">
					<div id="bcc" style="width:100%;height:95px;overflow:hidden">
						<select size="6" style="height:100%;width:100%;"></select>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>